package com.bjpowernode.controller;

import com.bjpowernode.beans.Type;
import com.bjpowernode.dto.ResultDTO;
import com.bjpowernode.exception.CRUDException;
import com.bjpowernode.services.TypeServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;

@RestController
@RequestMapping("/DictionaryType")
public class TypeController {

    @Autowired
    TypeServices typeServices;

    @RequestMapping("getAll.action")
    public ArrayList<Type> getAll() {
        ArrayList<Type> all = typeServices.getAll();
        return all;
    }

    @RequestMapping("add.action")
    public ResultDTO add(Type type) throws CRUDException {

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        boolean add = typeServices.add(type);
        if (!add) {
            throw new CRUDException("数据字典类型添加失败,影响行数没有变化");
        }
        return resultDTO;
    }

    @RequestMapping("editBefore.action")
    public Type editBefore(String id){
        return  typeServices.get(id);
    }


    @RequestMapping("edit.action")
    public ResultDTO edit(Type type) throws CRUDException {

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        boolean edit = typeServices.edit(type);
        if (!edit) {
            throw new CRUDException("数据字典类型修改失败,影响行数没有变化");
        }
        return resultDTO;
    }

    @RequestMapping("del.action")
    public ResultDTO edit(@RequestBody String[] ids) throws CRUDException {
        //System.out.println("xxxxxxxxxxx:"+ Arrays.toString(ids));
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        boolean del =  typeServices.del(ids);
        if (!del) {
            throw new CRUDException("数据字典类型修改失败,影响行数没有变化");
        }
        return resultDTO;
    }


}
